package com.example.option;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVObject;
import com.avos.avoscloud.AVQuery;
import com.example.tools.CommonUtils;
import com.example.tools.FileUtil;
import com.example.tools.HttpServletUtil;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * Created by yangyazhou on 2016/12/26.
 */

public class Option1 {

    public static void downPage() {
        try {
            String urlStart = "http://www.yuhuagu.com/yj/list_63431_";
            String urlEnd = ".html";

            String fileStart = "/Users/yangyazhou/self/pages/www.yuhuagu.com/list_63431_";
            String fileEnd = ".html";

            for (int i = 1; i <= 29; i++) {
                String html = HttpServletUtil.request(urlStart + i + urlEnd);
                String filePath = fileStart + i + fileEnd;
                FileUtil.saveAsFileOutputStream(filePath, html);
            }
            CommonUtils.log("下完啦！");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public static void test() throws Exception {
        String fileStart = "/Users/yangyazhou/self/pages/www.yuhuagu.com/list_63431_";
        String fileEnd = ".html";

        int index = 1;
        for (int i = 1; i <= 29; i++) {
            File input = new File(fileStart + i + fileEnd);
            Document doc = Jsoup.parse(input, "UTF-8", "http://www.yuhuagu.com/");

            Elements links = doc.select("#pnavv span a");
            for (Element element : links) {
                String linkHref = element.attr("href");
                String linkText = element.text();

                String s = "/Users/yangyazhou/self/pages/www.yuhuagu.com/second/";
                String e = ".html";

                CommonUtils.log("开始下载:" + linkText);
                downloadPageAndSave(s + (index++) + e, linkHref);
                CommonUtils.log("下载完成:" + linkText);
            }
        }
        CommonUtils.log("============全部下载完成============");
    }

    public static void parseAllPage() throws Exception {
        String fileStart = "/Users/yangyazhou/self/pages/www.yuhuagu.com/second/";
        String fileEnd = ".html";
        ArrayList<AVObject> list = new ArrayList<>();
        for (int i = 1; i <= 564; i++) {
            AVObject object = parsePage(fileStart + i + fileEnd);
            if (null != object) {
                list.add(object);
            }
        }
        CommonUtils.log("============全部解析完成============");
        CommonUtils.log("============开始保存全部Botany============");
        AVObject.saveAll(list);
        CommonUtils.log("============成功保存全部Botany============");

        Thread.sleep(3000);

        CommonUtils.log("============开始保存全部KeyName============");
        saveAllKey(keys);
        CommonUtils.log("============成功保存全部KeyName============");
    }

    public static AVObject parsePage(String filePath) throws Exception {
        try {
            File input = new File(filePath);
            Document doc = Jsoup.parse(input, "UTF-8", "http://www.yuhuagu.com/");

            Elements tbodys = doc.select(".wenz_tou tbody");
            Element tbody = tbodys.get(0);
            Elements trs = tbody.select("tr");

            AVObject botany = new AVObject("Botany");
            for (Element tr : trs) {
                Elements tds = tr.select("td");
                boolean isKey = true;
                String key = "";
                String value = "";
                for (Element td : tds) {
                    if (isKey) {
                        key = td.text().trim();
                    } else {
                        value = td.text().trim();
                        String theKey = getKey(key);
                        botany.put(theKey, value);
                    }
                    isKey = !isKey;
                }
            }
            Elements ps = doc.select(".wenz p");
            JSONArray jsonArray = new JSONArray();
            for (Element p : ps) {
                JSONObject jsonObject = new JSONObject();
                Element a = p.select("a").first();
                Element span = p.select("span").first();
                jsonObject.put("desc", span.text());
                jsonObject.put("url", a.attr("href"));
                jsonArray.add(jsonObject);
            }
            String theKey = getKey("图片");
            botany.put(theKey, jsonArray);
            CommonUtils.log("======解析了一个品种======");
            return botany;
        } catch (Exception e) {
            e.printStackTrace();
            CommonUtils.log("********解析失败：" + filePath);
        }
        return null;
    }


    public static void downloadPageAndSave(String desPath, String srcUrl) {
        String html = HttpServletUtil.request(srcUrl);
        FileUtil.saveAsFileOutputStream(desPath, html);
    }

    private static HashMap<String, String> keys = new HashMap<>();

    public static String getKey(String desc) throws Exception {
        if (keys.containsKey(desc)) {
            return keys.get(desc);
        } else {
            keys.put(desc, CommonUtils.getMD5(desc));
            return keys.get(desc);
        }
    }

    private static void saveAllKey(HashMap<String, String> keys) throws AVException {
        List<AVObject> keyNames = new ArrayList<>();
        Iterator iter = keys.entrySet().iterator();
        while (iter.hasNext()) {
            Map.Entry entry = (Map.Entry) iter.next();
            Object key = entry.getKey();
            Object value = entry.getValue();

            AVObject keyName = new AVObject("KeyName");
            keyName.put("key", value);
            keyName.put("desc", key);

            keyNames.add(keyName);
        }
        AVObject.saveAll(keyNames);
    }
}
